﻿from google.appengine.ext import db
from app.engine import models
from datetime import datetime
import hashlib,urllib

class Members(db.Model):
    user = db.UserProperty(auto_current_user_add=True)
    username = db.StringProperty()
    gender = db.StringProperty(required=False)
    first_login = db.DateTimeProperty(auto_now_add=True)
    last_login = db.DateTimeProperty(auto_now=True)
    account = db.StringProperty()
    homepage = db.LinkProperty(required=False)
    email = db.EmailProperty(required=False)
    summary = db.StringProperty()
    player = db.ReferenceProperty(models.Players)
    avaster = db.BlobProperty()
    thumbnail = db.BlobProperty()
    
class Topic(db.Model):
    title = db.StringProperty()
    content = db.TextProperty()
    author = db.ReferenceProperty(Members)
    post_on = db.DateTimeProperty(auto_now_add=True)
    edit_on = db.DateTimeProperty(auto_now=True)
    sequence = db.IntegerProperty()
    queue = db.DateTimeProperty()

    def post_topic(self):
        self.queue = self.post_on
        self.put()

    def get_last_reply(self):
        try:
            reply = Reply.all().filter('topic =', self).order('-post_on').fetch(1)
            reply = reply[0]
            return reply
        except:
            return None
     
    def get_reply_count(self):
        try:
            replies = Reply.all().filter('topic =', self.key())
            return replies.count()
        except:
            return 0

class Reply(db.Model):
    topic = db.ReferenceProperty(Topic)
    reply = db.SelfReferenceProperty(required=False)
    content = db.TextProperty()
    author = db.ReferenceProperty(Members)
    post_on = db.DateTimeProperty(auto_now_add=True)
    edit_on = db.DateTimeProperty(auto_now=True)

    def put_reply(self):
        self.topic.queue = self.post_on
        self.topic.put()
        self.put()

class Twitters(db.Model):
    twitter = db.StringProperty()
    post_on = db.DateTimeProperty(auto_now_add=True)
    author = db.ReferenceProperty(Members)

class Quotations(db.Model):
    quotation = db.StringProperty()
    post_on = db.DateTimeProperty(auto_now_add=True)
    author = db.ReferenceProperty(Members)

class iMail(db.Model):
    from_user = db.ReferenceProperty()
    to_user = db.ReferenceProperty(Members)
    title = db.StringProperty()
    content = db.StringProperty()
    post_on = db.DateTimeProperty(auto_now_add=True)